package com.ldzy.interceptor;

import com.ldzy.pojo.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        String url=request.getRequestURI();//获取请求的路径
        System.out.println("执行了拦截器");
        System.out.println(url);
        User user=(User) session.getAttribute("user_session");
        if(user!=null){return true;}//如果用户已登录，放行
        if(url.indexOf("/login")>=0){
            System.out.println("该请求放行");
            return true;}//用户登录的相关请求放行
        //返回为假，表示该用户未登录，重定向登录页面
        request.setAttribute("msg","您还没有登录");
        request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        return false;
    }
}
